package com.zzb.projectfactory.framework.event.controller;

import com.zzb.projectfactory.framework.common.BaseController;
import com.zzb.projectfactory.framework.event.entity.SysEventNotify;
import com.zzb.projectfactory.framework.role.entity.User;
import com.zzb.projectfactory.framework.event.form.SysEventNotifySearchForm;
import com.zzb.projectfactory.framework.event.service.SysEventNotifyService;
import com.zzb.projectfactory.framework.common.BasePage;
import com.zzb.projectfactory.framework.common.BaseResponse;
import com.zzb.projectfactory.framework.common.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
* 事件通知方法controller
*/
@Controller
@RequestMapping("/system/sysEventNotify")
public class SysEventNotifyController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(SysEventNotifyController.class);
    @Autowired
    private SysEventNotifyService sysEventNotifyService;

    /**事件通知方法管理首页*/
    @RequestMapping({"","/","/index"})
    public String index(){
        return "system/sysEventNotify/index";
    }
    /**事件通知方法列表json*/
    @RequestMapping("list")
    @ResponseBody
    public BasePage<SysEventNotify> list(SysEventNotifySearchForm sysEventNotifySearchForm){
        return sysEventNotifyService.queryPage(sysEventNotifySearchForm, SysEventNotify.class);
    }

    /**事件通知方法表单页面*/
    @RequestMapping("form")
    public String form(String id, Model model){
        SysEventNotify sysEventNotify;
        if(StringUtils.isEmpty(id)){
            sysEventNotify = new SysEventNotify();
    } else{
        sysEventNotify = sysEventNotifyService.get(id);
        if(sysEventNotify == null) sysEventNotify = new SysEventNotify();
    }
        model.addAttribute("sysEventNotify",sysEventNotify);
        return "system/sysEventNotify/form";
    }

    /**事件通知方法保存*/
    @RequestMapping("/save")
    @ResponseBody
    public BaseResponse<SysEventNotify> save(SysEventNotify sysEventNotify , HttpServletRequest request){
        try {
            User user = getCurrentUser(request);
            return sysEventNotifyService.saveSysEventNotify(sysEventNotify, user);
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse<>(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**事件通知方法删除*/
    @RequestMapping("/delete")
    @ResponseBody
    public BaseResponse delete(String id,HttpServletRequest request){
        try {
            return sysEventNotifyService.deleteSysEventNotify(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**事件通知方法开启*/
    @RequestMapping("/open")
    @ResponseBody
    public BaseResponse open(String id ,HttpServletRequest request){
        try {
            return sysEventNotifyService.recovery(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**事件通知方法屏蔽*/
    @RequestMapping("/deny")
    @ResponseBody
    public BaseResponse deny(String id ,HttpServletRequest request){
        try {
            return sysEventNotifyService.trash(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

}
